/*
	File:		CCreateSpreadLaserMessage.cpp
	Course:		SGD 1301
	Author:		Peter Nguyen
	Purpose:	Message to create a multiple laser entity
*/

#include "CreateSpreadLaserMessage.h"

#include "Laser.h"
#include "../SGD Wrappers/SGD_Math.h"
#include "GameplayState.h"
#include "EntityManager.h"

////////////////////////////////////////////////////////////
// CONSTRUCTOR
CCreateSpreadLaserMessage::CCreateSpreadLaserMessage(CEntity* _pOwner, float fPosX, float fPosY, float fRotation )
	: CMessage( MSG_CREATE_SPREAD_LASER )		// call parent's constructor
{
	// Store the parameters
	m_fPosX		= fPosX;
	m_fPosY		= fPosY;
	m_fRotation	= fRotation;
	m_pOwner	= _pOwner;
	_pOwner->AddRef();
}


////////////////////////////////////////////////////////////
// DESTRUCTOR
CCreateSpreadLaserMessage::~CCreateSpreadLaserMessage(void)
{
	m_pOwner->Release();
}

//create and store bullets into entitymanager.
void CCreateSpreadLaserMessage::Process( void ) const
{
	CEntityManager* _pEM = CEntityManager::GetInstance();
	CGameplayState* _pGS = CGameplayState::GetInstance();
	
	for(int i = -1; i <= 1; ++i)
	{
		CLaser* pBullet = new CLaser();
		pBullet->SetOwner(m_pOwner);

		pBullet->SetWidth(32);
		pBullet->SetHeight(64);
		pBullet->SetRotation(m_pOwner->GetRotation()+i*0.3f);

		tVector2D forwardVector = {0,-1};
		forwardVector = Vector2DRotate(forwardVector,pBullet->GetRotation());
		
		pBullet->SetPosX(m_pOwner->GetPosX());//+forwardVector.fX*m_pOwner->GetWidth()*0.5f);//force to shoot at nose
		pBullet->SetPosY(m_pOwner->GetPosY());//+forwardVector.fY*m_pOwner->GetHeight()*0.5f);//force to shoot at nose

		forwardVector = forwardVector * 950;

		if(m_pOwner->GetSpeed()>0)
		{
			forwardVector.fX += m_pOwner->GetVelX();
			forwardVector.fY += m_pOwner->GetVelY();
		}

		pBullet->SetVelX(forwardVector.fX);
		pBullet->SetVelY(forwardVector.fY);

		pBullet->SetImageID(_pGS->m_nLaserImageID);

		_pEM->AddEntity( pBullet );

		pBullet->Release();
		pBullet = nullptr;
	}
}
